﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace library
{
    public partial class lost_disposal : Form
    {
        string bID, bName;
        int pubYear;
        double Price;
        public MySqlConnection conn = new DBclass().getConn();
        public lost_disposal(string bid,string bname, int pubyear,double price)
        {
            InitializeComponent();
            this.bID = bid;
            this.bName = bname;
            this.pubYear = pubyear;
            this.Price = price;

        }
        private void button15_Click(object sender, EventArgs e)
        {
            conn.Open();
            string bID = textBox5.Text;
            string uname = label16.Text;
            string app_time = dateTimePicker2.Text;
            string app_reason = textBox6.Text;
            if (bID.Equals(""))
            {
                MessageBox.Show("bID不能为空！");
            }
            else
            { 
                string sql = "insert into pay_info(bID,uname,app_time,app_reason) values ('" + bID + "','" + uname + "','" + app_time + "','" + app_reason + "')";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                string sql1 = "update book_info set lose_whether='是' where bID=@bID";
                MySqlCommand cmd1 = new MySqlCommand(sql1, conn);
                cmd1.Parameters.Add("@bID", MySqlDbType.Int32).Value = bID;
                int num = cmd1.ExecuteNonQuery();
                if (num > 0)
                {
                    MessageBox.Show("提交成功");
                }
            }
            conn.Close();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            string uname = label16.Text;
            conn.Open();
            string sql = "select bID as 图书编号,uname as 申请人,app_time as 申请时间,app_reason as 申请理由,pay_status as 审核状态 from pay_info where uname=@uname";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter sd = new MySqlDataAdapter(cmd);
            cmd.Parameters.Add("@uname", MySqlDbType.VarChar).Value = uname;
            DataSet ds = new DataSet();
            sd.Fill(ds, "pay_info");
            dataGridView7.DataSource = ds;
            dataGridView7.DataMember = "pay_info";
            conn.Close();
        }
        private void button7_Click(object sender, EventArgs e)
        {
            conn.Open();
            string sql = "select uID as 用户ID,bID as 图书编号, fee as 罚款金额,fee_reason as 罚款原因,pay_whether as 是否缴清 from fine_info where uID=@uID";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter sd = new MySqlDataAdapter(cmd);
            cmd.Parameters.Add("@uID", MySqlDbType.Int32).Value = User.uID;
            DataSet ds = new DataSet();
            sd.Fill(ds, "fine_info");
            dataGridView8.DataSource = ds;
            dataGridView8.DataMember = "fine_info";
            conn.Close();
        }
        private void button8_Click(object sender, EventArgs e)
        {
            conn.Open();
            string sql1 = "select pay_whether='否' from fine_info where uID =@uID";
            MySqlCommand cmd1 = new MySqlCommand(sql1, conn);
            cmd1.Parameters.Add("@uID", MySqlDbType.Int32).Value = User.uID;
            MySqlDataReader sdr = cmd1.ExecuteReader();
            if (!sdr.HasRows)
            {
                MessageBox.Show("您暂时没有要缴费的项目");
            }
            else
            {
                sdr.Close();
                string sql = "update fine_info set pay_whether='是' where uID=@uID";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.Add("@uID", MySqlDbType.Int32).Value = User.uID;
                int num = cmd.ExecuteNonQuery();
                if (num > 0)
                {
                    MessageBox.Show("缴费成功");
                }
                /*string sql3 = "select bID from fine_info where uID =@uID";
                MySqlCommand cmd3 = new MySqlCommand(sql3, conn);
                cmd3.Parameters.Add("@uID", MySqlDbType.Int32).Value = User.uID;*/
                string sql2 = "update borrow_info set return_whether='1' where uID=@uID";
                MySqlCommand cmd2 = new MySqlCommand(sql2, conn);
                //cmd2.Parameters.Add("@bID", MySqlDbType.Int32).Value = bID;
                cmd2.Parameters.Add("@uID", MySqlDbType.Int32).Value = User.uID;
                //j += cmd.ExecuteNonQuery();
                int num2 = cmd1.ExecuteNonQuery();
                if (num2 > 0)
                {
                    MessageBox.Show("修改成功");
                }
            }
            sdr.Close();
            conn.Close();
        }
        private void lost_disposal_Load(object sender, EventArgs e)
        {
            label16.Text = User.uname.ToString();
            conn.Open();
            string sql = "select * from fine_rule where rname='赔书罚金'";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            double mag = Convert.ToDouble(dataTable.Rows[0]["mag"]);
            int year = DateTime.Now.Year;
            double fee = (pubYear - year)/mag*Price;
            textBox1.Text = bID;
            textBox2.Text = bName;
            textBox3.Text = fee.ToString();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            textBox5.Clear();
            textBox6.Clear();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            conn.Open();
            string bID = textBox1.Text;
            string bname = textBox2.Text;
            string fee = textBox3.Text;
            string sql = "insert into fine_info(uID,bID,fee,fee_reason) values(@uID,@bID,@fee,'赔书')";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.Parameters.Add("@uID", MySqlDbType.Int32).Value = User.uID;
            cmd.Parameters.Add("@bID", MySqlDbType.VarChar).Value = bID;
            cmd.Parameters.Add("@fee", MySqlDbType.Double).Value = fee;
            int num = cmd.ExecuteNonQuery();
            if (num > 0)
            {
                MessageBox.Show("提交成功");
            }
            conn.Close();
        }
    }
}
